//
//  SignatureViewController.m
//  Today
//
//  Created by fanfan on 15/5/22.
//  Copyright (c) 2015年 fanfan. All rights reserved.
//

#import "SignatureViewController.h"

@interface SignatureViewController ()

@end

@implementation SignatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activity.color = [UIColor grayColor];
    activity.frame = CGRectMake(0, 0, 200, 200);
    activity.center = self.view.center;
    [self.view addSubview:activity];
    [activity startAnimating];
    self.navigationItem.title = @"一句话签名";
    [self loadrequest];
    UIImage *image = [UIImage imageNamed:@"iconfont-jiahao"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(selectorMy)];
    UIImage *image1 = [UIImage imageNamed:@"iconfont-fanhui-2"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image1 style:UIBarButtonItemStyleDone target:self action:@selector(Back)];
    
    // Do any additional setup after loading the view.
}
- (void)selectorMy
{
    SignatureDateBaseViewController *database = [[SignatureDateBaseViewController alloc]init];
    [self.navigationController pushViewController:database animated:YES];
    
    
}
- (void)Back
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)Mytableview
{
    tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:tableview];
    tableview.dataSource = self;
    tableview.delegate = self;
    [tableview release];
    
    [tableview addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(xiala)];
    [tableview addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(shangla)];
    
}
- (void)xiala
{
    NSString *urlpath = @"http://qianming.sinaapp.com/index.php/AndroidApi23/index/cid/qutu/p/1/markId/0";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *params = @{@"page" : @"2"};//表示第几页
    
    [manager GET:urlpath parameters:params success:
     
     ^(AFHTTPRequestOperation *operation, id responseObject) {
         NSData *data = operation.responseData;
         NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
         NSArray *array = [dic objectForKey:@"rows"];
         self.array = [NSMutableArray array];
         for (NSDictionary *dic1 in array) {
             SignatureModel *Model = [SignatureModel getSignatureModel];
             Model.title = [dic1 objectForKey:@"title"];
             Model.pic = [dic1 objectForKey:@"pic"];
             Model.timeStr = [dic1 objectForKey:@"timeStr"];
             Model.pic_h = [dic1 objectForKey:@"pic_h"];
             [self.array addObject:Model];
             [self Mytableview];
             
         }
     }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             
         }];
    [tableview.header endRefreshing];
    
}
- (void)shangla
{
    NSString *urlpath = @"http://qianming.sinaapp.com/index.php/AndroidApi23/index/cid/qutu/p/1/markId/0";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *params = @{@"page" : @"2"};//表示第几页
    
    [manager GET:urlpath parameters:params success:
     
     ^(AFHTTPRequestOperation *operation, id responseObject) {
         NSData *data = operation.responseData;
         NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
         NSArray *array = [dic objectForKey:@"rows"];
         self.array = [NSMutableArray array];
         for (NSDictionary *dic1 in array) {
             SignatureModel *Model = [SignatureModel getSignatureModel];
             Model.title = [dic1 objectForKey:@"title"];
             Model.pic = [dic1 objectForKey:@"pic"];
             Model.timeStr = [dic1 objectForKey:@"timeStr"];
             Model.pic_h = [dic1 objectForKey:@"pic_h"];
             [self.array addObject:Model];
             [self Mytableview];
             
         }
     }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             
         }];
    [tableview.footer endRefreshing];
}
//加载内容（网络请求， 解析）
-(void)loadrequest
{
    NSString *urlpath = @"http://qianming.sinaapp.com/index.php/AndroidApi23/index/cid/qutu/p/1/markId/0";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *params = @{@"page" : @"2"};//表示第几页
    
    [manager GET:urlpath parameters:params success:
     
     ^(AFHTTPRequestOperation *operation, id responseObject) {
         NSData *data = operation.responseData;
         NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
         NSArray *array = [dic objectForKey:@"rows"];
         self.array = [NSMutableArray array];
         for (NSDictionary *dic1 in array) {
             SignatureModel *Model = [SignatureModel getSignatureModel];
             Model.title = [dic1 objectForKey:@"title"];
             Model.pic = [dic1 objectForKey:@"pic"];
             Model.timeStr = [dic1 objectForKey:@"timeStr"];
             Model.pic_h = [dic1 objectForKey:@"pic_h"];
             [self.array addObject:Model];
             [self Mytableview];
             
         }
     }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             
             
         }];
    
}
- (void)shareContent:(NSInteger)myTag
{
    SignatureModel *model1 = [self.array objectAtIndex:myTag];
    NSURL *url = [NSURL URLWithString:model1.pic];
    NSData *data = [NSData dataWithContentsOfURL:url];
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5565371467e58e202a001de2"
                                      shareText:model1.title
                                     shareImage:[UIImage imageWithData:data]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil]
                                       delegate:self];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"cell";
    SignatureTableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[SignatureTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]autorelease];
    }
    cell.Mydeleget = self;
    [cell.SharePressBut setTag:(1000 + indexPath.row)];
    SignatureModel *Model = [self.array objectAtIndex:indexPath.row];
    cell.Model = Model;
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PicViewController *first = [[PicViewController alloc]init];
    [self.navigationController pushViewController:first animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
